using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class IMUController : MonoBehaviour
{
    private bool isG200 = false;

    private Camera maincamera;

#if UNITY_ANDROID
    private static AndroidJavaObject m_Activity = null;
    private static AndroidJavaObject JO
    {
        get
        {
            if (m_Activity == null)
            {
                using (AndroidJavaObject unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
                {
                    m_Activity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
                }
            }
            return m_Activity;
        }
    }

#endif
     private float[] mGetIMUPose()
        {
            if (JO == null)
            {
                Debug.LogError("jo is null !!");
                return null;
            }
            float[] targetArray = JO.Call<float[]>("GetIMUValue");
            return targetArray;
    }



    /// <summary>
    /// Awake is called when the script instance is being loaded.
    /// </summary>
    void Awake()
    {   
        isG200 = SystemInfo.deviceModel.Equals("Hiscene G200");
        maincamera = GetComponent<Camera>();
    }

    private float[] m_poseArray = new float[16];

    // Update is called once per frame
    void Update()
    {
        #if UNITY_ANDROID

       m_poseArray = mGetIMUPose();

       Matrix4x4 posMatrix = MathFunctions.MatrixFromFloatArray(m_poseArray);
       if(isG200){
         Matrix4x4 martrixRot_Right = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(Vector3.right * 270.0f), Vector3.one);
         posMatrix = posMatrix *martrixRot_Right;
         Matrix4x4 posMatrix_Unity = MathFunctions.LHMatrixFromRHMatrix(posMatrix);
         Vector3 eA =  MathFunctions.QuaternionFromMatrix(posMatrix_Unity).eulerAngles;
         var rotation = Quaternion.Euler(-eA.y,eA.z,eA.x);
         transform.rotation = rotation;
       }else{
          Matrix4x4 posMatrix_Unity = MathFunctions.LHMatrixFromRHMatrix(posMatrix);
          Vector3 eA =  MathFunctions.QuaternionFromMatrix(posMatrix_Unity).eulerAngles;
          var rotation = Quaternion.Euler(-eA.x,-eA.y,-eA.z);
          transform.rotation = rotation;
       }
        
        #endif
    }

}
